package com.ruoyi.livedemo.mapper;

import com.ruoyi.livedemo.entity.UserSession;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;

@Mapper
public interface UserSessionMapper {

    @Insert("INSERT INTO user_sessions (user_id, token, issued_at, expires_at, user_agent) " +
            "VALUES (#{userId}, #{token}, #{issuedAt}, #{expiresAt}, #{userAgent})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(UserSession session);

    @Select("SELECT * FROM user_sessions WHERE token = #{token}")
   UserSession findByToken(String token);

    @Select("SELECT MAX(issued_at) FROM user_sessions WHERE user_id=#{userId}")
    LocalDateTime findLatestLogin(Long userId);

    @Delete("DELETE FROM user_sessions WHERE token = #{token}")
    int deleteByToken(String token);

    @Delete("DELETE FROM user_sessions WHERE expires_at IS NOT NULL AND expires_at < #{now}")
    int deleteExpired(LocalDateTime now);
}


